package com.yaocw.baseproject.activity;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.yaocw.baseproject.R;
import com.yaocw.baseproject.view.NavigationBar;

public class RegisterActivity extends BaseActivity {

    private static int timeCount;

    private NavigationBar navigationBar;
    private Button btnFetchCheckcode;

    private Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what > 0) {
                btnFetchCheckcode.setText(msg.what + "秒");
            } else {
                btnFetchCheckcode.setText("获取验证码");
                btnFetchCheckcode.setEnabled(true);
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        navigationBar = findViewById(R.id.navigation_bar);
        navigationBar.setTitle("注册");
        navigationBar.setLeftButton(null, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btnFetchCheckcode = findViewById(R.id.btn_fetch_checkcode);
        btnFetchCheckcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btnFetchCheckcode.setEnabled(false);
                btnFetchCheckcode.setText("300秒");
                timeCount = 300;

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            timeCount--;
                            if (timeCount < 0) {
                                break;
                            }
                            try {
                                Thread.sleep(1000);

                                Message message = new Message();
                                message.what = timeCount;
                                handler.sendMessage(message);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });
    }

    @Override
    protected void initData() {

    }
}